//
//  CmgeBaseViewController.m
//  CmgeIosClient
//
//  Created by Gang.Liu on 14-3-24.
//  Copyright (c) 2014年 zhouqing. All rights reserved.
//

#import "LGBaseViewController.h"

#import <UIViewController+MMDrawerController.h>
#import <MMDrawerBarButtonItem.h>

@interface LGBaseViewController ()

@property (nonatomic, strong) UIView * darkView;
@property (nonatomic, strong) UIActivityIndicatorView * indicatorView;

@end

@implementation LGBaseViewController

- (void)addBackButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:@"backBtn"] forState:UIControlStateNormal];
    [button sizeToFit];
    [button addTarget:self action:@selector(onBackButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)addRightImageButton:(NSString *)imageName
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button sizeToFit];
    [button addTarget:self action:@selector(onRightButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

- (void)addRightStringButton:(NSString *)buttonTitle
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
//    [button setTitleColor:color forState:UIControlStateNormal];
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button sizeToFit];
    [button addTarget:self action:@selector(onRightButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

- (void)addLeftStringButton:(NSString *)buttonTitle
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
//    [button setTitleColor:color forState:UIControlStateNormal];
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button sizeToFit];
    [button addTarget:self action:@selector(onLeftButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = rightBarButton;
}

-(void)setupLeftBarButton
{
    MMDrawerBarButtonItem * leftButton = [[MMDrawerBarButtonItem alloc] initWithTarget:self
                                                                                action:@selector(leftDrawerButtonPress)];
    [leftButton setImage:[UIImage imageNamed:@"left_nav_btn"]];
    [leftButton setTintColor:[UIColor whiteColor]];
    [self.navigationItem setLeftBarButtonItem:leftButton animated:YES];
}

-(void)setCustomTitle:(NSString *)title
{
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 120, 44)];
    label.font = [UIFont boldSystemFontOfSize:20];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = title;
    self.navigationItem.titleView = label;
}

-(void)setCustomTitleWithView:(UIView *)titleView
{
    self.navigationItem.titleView = titleView;
    
}

-(void)showTabbar
{
    self.tabBarController.tabBar.hidden = NO;
    
}

-(void)hideTabbar
{
    self.tabBarController.tabBar.hidden = YES;
    
}

-(void)configNavigationBar:(NSString *)title
                hasBackBtn:(BOOL)hasBackBtn
{
    [self setCustomTitle:title];
    // TODO ios 7
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        [self.navigationController.navigationBar setBarTintColor:[UIColor red:22 green:187 blue:92 alpha:1]];
    }
}

-(void)startIndicator
{
    self.darkView = [[UIView alloc] initWithFrame:self.view.bounds];
    
    self.darkView.backgroundColor = [UIColor grayColor];
    self.darkView.alpha = 0.5f;
    [self.view addSubview:self.darkView];
    
    self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.indicatorView.center = self.darkView.center;
    [self.indicatorView startAnimating];
    [self.view addSubview:self.indicatorView];
}

-(void)stopIndicator
{
    [self.darkView removeFromSuperview];
    [self.indicatorView stopAnimating];
    [self.indicatorView removeFromSuperview];
}

#pragma mark - lifeCycle

- (void)viewWillDisappear:(BOOL)animated
{
//    [super viewWillDisappear:animated];
    [self hidesBottomBarWhenPushed];
}

- (void)viewWillAppear:(BOOL)animated
{
//    [super viewWillAppear:animated];
    [self showTabbar];
}

#pragma mark - Actions

- (void)onBackButtonPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)back
{
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(void)onRightButtonPressed
{
    
}

- (void)onLeftButtonPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Navigation

- (void)leftDrawerButtonPress
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

@end
